﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XYTXL.AppCode;
using XYTXL.Data;
using XYTXL.API;

namespace XYTXL.View
{
    public partial class LXQuery : QueryUControl
    {


        public string strFormCode
        {
            get { return Request.QueryString["FormCode"] ?? ""; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            if (!string.IsNullOrEmpty(FormBaseData.FormTip))
            {
                tbSearch.Attributes.Add("placeholder", "可输入<" + FormBaseData.FormTip + ">进行查询");
            }
            if (!IsPostBack)
            {
                new CommonHelp.BindData().BindXWLX(ddlCGLX);
                ddlCGLX.Items.Insert(0, new ListItem("全部类型", "0"));

            }


        }
        public override string SetDefaultQuery()
        {
            return " AND Status='0' ";
        }
        public override string GetQuerydata()
        {
            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            string strSearchWhere = GetSearchWhere(tbSearch.Value.Trim(), FormBaseData.FormSearchColumn);


            if (ddlCGLX.SelectedValue != "0")
            {
                strSearchWhere += string.Format(" And  Type = '{0}'", ddlCGLX.SelectedValue);
            }
            return strSearchWhere;

        }
        public string GetSearchWhere(string strSearchKeyWord, string strSearchColumu)
        {
            string strRetur = "";
            if (strSearchKeyWord.Trim() != "")
            {
                string[] SearchKeywords = System.Text.RegularExpressions.Regex.Split(strSearchKeyWord, @"\s+");
                for (int i = 0; i < SearchKeywords.Length; i++)
                {
                    foreach (string strColumn in strSearchColumu.Split(','))
                    {
                        strRetur = strRetur + (strRetur == "" ? "" : "+") + (strColumn.Contains("Date") ? "CONVERT(varchar(100), " + strColumn + ", 23)" : strColumn);
                    }
                    strRetur = " AND " + strRetur + " LIKE '%" + SearchKeywords[i].ToString() + "%' ";
                }
            }

            return strRetur;
        }

        protected void ddlCGLX_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommonListN com = (CommonListN)this.Page;
            com.querydata(GetQuerydata());
        }
        protected void ddlZT_SelectedIndexChanged(object sender, EventArgs e)
        {
            CommonListN com = (CommonListN)this.Page;
            com.querydata(GetQuerydata());
        }


    }
}